package com.mlj.service;

import com.mlj.dao.StudentMapper;
import com.mlj.pojo.Student;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StudentServiceImpl implements StudentService{
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> getStudentName() {
        return studentMapper.selectAll();
    }

    @Override
    public Result getStudentList(Integer page, Integer limit) {
        //计算分页查询的起始位置
        Integer start = (page-1)*limit;
        Result result = new Result();
        //分页查询列表
        List<Student> students = studentMapper.findStudentListByPage(start,limit);
        result.setItem(students);//获取数据
        Integer count = studentMapper.selectCount();
        result.setTotal(count);//数据长度
        return result;
    }

    @Override
    public Result deleteBatch(Integer[] ids) {
        Result result = new Result();
        try{
            studentMapper.deleteBatch(ids);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result insertStudent(String studentName, String studentCode, String studentTelephone, Integer studentSex, String studentEnrolment, String studentClass, Integer studentStatus) {
        Result result = new Result();
        try{
            studentMapper.insertStudent(studentName ,studentCode ,studentTelephone,studentSex,studentEnrolment ,studentClass ,studentStatus);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，添加失败");
        }
        return result;
    }

    @Override
    public Result delStudentById(Integer id) {
        Result result = new Result();
        try{
            studentMapper.delStudentById(id);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，删除失败");
        }
        return result;
    }

    @Override
    public Result updateStudentById(Student student) {
        Result result = new Result();
        try{
            studentMapper.updateStudentById(student);
            result.setStatus(200);//自己编的响应码 200就代表成功
            result.setMessage("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setStatus(500);//自己编的响应码，代表错误
            result.setMessage("网络延迟，更新失败");
        }
        return result;
    }

    @Override
    public Result search(String timerange, String keyword, Integer page, Integer limit) {
        Result result = new Result();
        //使用map存储传递的五个参数
        Map<String,Object> paraMaps = new HashMap<>();
        //计算分页查询起始位置
        Integer start = (page-1)*limit;
        paraMaps.put("start",start);
        paraMaps.put("limit",limit);
        paraMaps.put("keyword",keyword);
        //判断时间范围是否为空字符串
        if(!timerange.equals("")){
            //根据开始和结束时间查询
            //2015~2020   使用split进行切割，返回数组
            String [] timeArray = timerange.split("~");
            //trim():去除字符串前后的空格 2015~2020 = 2015-01-01~2020-12-31
            paraMaps.put("startTime", timeArray[0].trim() + "-" + "01" + "-" + "01");
            paraMaps.put("endTime", timeArray[1].trim() + "-" + "12" + "-" + "31");
        }else{
            paraMaps.put("startTime",null);
            paraMaps.put("endTime",null);
        }
        //根据时间范围和关键字进行分页查询
        List<Student> searchStudents = studentMapper.searchStudentByTimeAndKw(paraMaps);
        result.setItem(searchStudents);
        //搜索视频的长度,便于搜索之后分页
        Integer count = studentMapper.searchCountByTimeAndKw(paraMaps);
        result.setTotal(count);
        return result;
    }
}
